using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class KiemSoatLien3DAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveKiemSoatLien3(KiemSoatLien3 kiemsoatlien3)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddKiemSoatLien3", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddKiemSoatLien3";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kiemsoatlien3.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuXuatHang", DbType.Int32, ParameterDirection.Input, kiemsoatlien3.IdPhieuXuatHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NgayBaoSoat", DbType.DateTime, ParameterDirection.Input, kiemsoatlien3.NgayBaoSoat);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiThucHien", DbType.String, ParameterDirection.Input, kiemsoatlien3.NguoiThucHien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@GhiChu", DbType.String, ParameterDirection.Input, kiemsoatlien3.GhiChu);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all KiemSoatLien3 in DB
		public ResultObj GetAllKiemSoatLien3( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KiemSoatLien3> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKiemSoatLien3", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KiemSoatLien3>();
			KiemSoatLien3 item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KiemSoatLien3();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
            if (reader["NgayBaoSoat"] != DBNull.Value)
			item.NgayBaoSoat = Convert.ToDateTime(reader["NgayBaoSoat"]);
			item.NguoiThucHien = Convert.ToString(reader["NguoiThucHien"]);
            item.GhiChu = Convert.ToString(reader["GhiChu"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all KiemSoatLien3 in DB with condition
		public ResultObj GetAllKiemSoatLien3ByID(KiemSoatLien3 kiemsoatlien3)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KiemSoatLien3> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllKiemSoatLien3ByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kiemsoatlien3.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KiemSoatLien3>();
			KiemSoatLien3 item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KiemSoatLien3();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
			item.NgayBaoSoat = Convert.ToDateTime(reader["NgayBaoSoat"]);
			item.NguoiThucHien = Convert.ToString(reader["NguoiThucHien"]);
            item.GhiChu = Convert.ToString(reader["GhiChu"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a KiemSoatLien3 out of DB with condition
		public ResultObj DeleteKiemSoatLien3(KiemSoatLien3 kiemsoatlien3)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteKiemSoatLien3", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteKiemSoatLien3";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, kiemsoatlien3.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhieuXuatHang from DB with condition
		public ResultObj GetPhieuXuatHang(int IdPhieuXuatHang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuXuatHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuXuatHang", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuXuatHang);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuXuatHang>();
			PhieuXuatHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuXuatHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KhachHang = Convert.ToString(reader["KhachHang"]);
			item.ChiNhanhKhachHang = Convert.ToString(reader["ChiNhanhKhachHang"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			item.HinhThucThanhToan = Convert.ToString(reader["HinhThucThanhToan"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.TongTienHang = Convert.ToInt64(reader["TongTienHang"]);
			item.VAT = Convert.ToInt64(reader["VAT"]);
			item.TienVAT = Convert.ToInt64(reader["TienVAT"]);
			item.ChietKhau = Convert.ToInt64(reader["ChietKhau"]);
			item.TienChietKhau = Convert.ToInt64(reader["TienChietKhau"]);
			item.TongCong = Convert.ToInt64(reader["TongCong"]);
			item.NguoiKiemTraHang = Convert.ToString(reader["NguoiKiemTraHang"]);
			item.NgayKiemTraHang = Convert.ToDateTime(reader["NgayKiemTraHang"]);
			item.XeChoHang = Convert.ToString(reader["XeChoHang"]);
			item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
			item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
	}
}
